import { createReducer } from '../utils/index'
import { FETCH_CART_GOODS_SUCC, FETCH_CART_GOODS_FAIL, ADD_GOODS_CART_SUCC, ADD_GOODS_CART_FAIL, DELETE_CART_GOODS_SUCC, DELETE_CART_GOODS_FAIL, CHANGE_GOODS_NUM_SUCC, CHANGE_GOODS_NUM_FAIL, CLEAN_CART_SUCC, CLEAN_CART_FAIL } from '../contants/shoppingCart'

const initialState = {
	goodsData: [],
    isFetched: null,
    numChanged: null,
    isCleaned: null,
    isDeleted: null,
    isAdded: null,
}

export default createReducer(initialState, {
	[FETCH_CART_GOODS_SUCC]: (state, payload) => {
        return Object.assign({}, state, {
            'isFetched': true,
            'isDeleted': null,
            'goodsData': payload.data
        })
    },

    [FETCH_CART_GOODS_FAIL]: (state, payload) => {
        return Object.assign({}, state, {
            'isFetched': false,
            'goodsData': []
        })
    },

    [ADD_GOODS_CART_SUCC]: (state,) => {
        return Object.assign({}, state, {
            'isAdded': true
        })
    },

    [ADD_GOODS_CART_FAIL]: (state) => {
        return Object.assign({}, state, {
            'isAdded': false
        })
    },

    [CHANGE_GOODS_NUM_SUCC]: (state) => {
        return Object.assign({}, state, {
            'numChanged': true
        })
    },

    [CHANGE_GOODS_NUM_FAIL]: (state) => {
        return Object.assign({}, state, {
            'numChanged': false
        })
    },

    [DELETE_CART_GOODS_SUCC]: (state) => {
        return Object.assign({}, state, {
        	'isDeleted': true
        })
    },

    [DELETE_CART_GOODS_FAIL]: (state) => {
        return Object.assign({}, state, {
        	'isDeleted': false
        })
    },

    [CLEAN_CART_SUCC]: (state) => {
        return Object.assign({}, state, {
            'isCleaned': true
        })
    },

    [CLEAN_CART_FAIL]: (state) => {
        return Object.assign({}, state, {
            'isCleaned': false
        })
    }
})